const dataAccess = require('../dataAccess');
const sysModelAttr = dataAccess["main"].model("sysModelAttr");
const sysModelBelong = dataAccess["main"].model("sysModelBelong");
const sysModel = dataAccess["main"].model("sysModel");

var mkdirByPath = require("../utils/mkdirByPath")
var modelQueryDis = require('../config').queryDisableAttr;
var modelFormDis = require('../config').formDisableAttr;
var modelTableRefDis = require('../config').tableRefDisableAttr;
var writeJsFile = require("../utils/writeJsFile")
async function getModelAttrs(model) {
    // console.log(model)
    // 字段列表查询
    var modelAttrList = await sysModelAttr.findAll({
        "where": {
            "modelId": model.modelId
        },
        "order": ['sortNo'],
    });
    // 外键列表查询
    var modelBelongList = await sysModelBelong.findAll({
        "where": {
            "modelId": model.modelId
        },
    });
    // 外键列表
    var fKeyList = []
    for (var j = 0; j < modelBelongList.length; j++) {
        var element = modelBelongList[j]
        var modelBel = element.get({ plain: true });
        modelBel["fKeyModel"] = await sysModel.findOne({
            "where": {
                "modelCode": element.tarModelCode
            },
        })
        fKeyList.push(modelBel);
    }
    var queryStr = "\t\t";
    var formElementStr = "\t\t";
    var tableStr = "\n\t\tindex: \"序号\",";
    for (var t = 0; t < modelAttrList.length; t++) {
        var modelAttr = modelAttrList[t].get({ plain: true });
        // 判断该字段是否是外键
        var fKeyStatus = false;
        var fKeyModel = {};
        var fKeyBel = {}
        for (var i = 0; i < fKeyList.length; i++) {
            if (fKeyList[i].fKey == modelAttr.modelAttrName) {
                fKeyStatus = true
                fKeyBel = fKeyList[i]
                fKeyModel = fKeyList[i].fKeyModel
            }
        }
        // 该字段不是主键
        if (modelAttr.pkFlag == "N") {
            // 不是不加入query的元素
            if (modelQueryDis.indexOf(modelAttr.modelAttrName) == -1) {
                if (modelAttr.modelAttrName != "delFlag") {
                    if (modelAttr.modelAttrName == "updateTime") {
                        queryStr = queryStr + "\n\t\t" + `updateTimeStart: '更新时间起',` + "\n\t\t" +
                            `updateTimeStartHolder: '请选择更新时间起',` + "\n\t\t" +
                            `updateTimeEnd: '更新时间止',` + "\n\t\t" +
                            `updateTimeEndHolder: '请选择更新时间止',`
                    } else {
                        if (fKeyStatus == true) {
                            if (fKeyModel && fKeyModel.modelName) {
                               
                                if(fKeyModel.modelCode=="com.xframe.domain.sys.SysValueDetail"){
                                    queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + ": " + "\"" + modelAttr.attrDesc + "\","
                                    queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + "Holder" + ": " + "\"请选择" + modelAttr.attrDesc + "\","
                                }else{
                                    queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + ": " + "\"" + fKeyModel.modelName + "\","
                                    queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + "Holder" + ": " + "\"请选择" + fKeyModel.modelName + "\","    
                                }
                                
                            }

                        } else {
                            queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + ": " + "\"" + modelAttr.attrDesc + "\","
                            queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + "Holder" + ": " + "\"请输入" + modelAttr.attrDesc + "\","
                        }
                    }
                } else {
                    queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + ": " + "\"" + modelAttr.attrDesc + "\","
                    queryStr = queryStr + "\n\t\t" + modelAttr.modelAttrName + "Holder" + ": " + "\"请选择" + modelAttr.attrDesc + "\","
                }
            }
            // 不是不加入form的元素
            if (modelFormDis.indexOf(modelAttr.modelAttrName) == -1) {
                if (modelAttr.modelAttrName != "delFlag") {

                    if (fKeyStatus == true) {
                        if (fKeyModel && fKeyModel.modelName) {
                            if(fKeyModel.modelCode=="com.xframe.domain.sys.SysValueDetail"){
                                formElementStr = formElementStr + modelAttr.modelAttrName + ": {\n\t\t\tlabel: \"" + modelAttr.attrDesc + "\"," + "\n\t\t\t"
                                formElementStr = formElementStr + "holder" + ": " + "\"请选择" + modelAttr.attrDesc + "\"," + "\n\t\t\t"
                                if (modelAttr.isRequired == "Y") {
                                    formElementStr = formElementStr + "rules: {" + "\n\t\t\t\t"
                                    formElementStr = formElementStr + "isRequired: " + "\"" + modelAttr.attrDesc + "必选\"," + "\n\t\t\t},\n\t\t},\n\t\t"
                                } else {
                                    formElementStr = formElementStr + "rules: {}" + "\n\t\t},"
                                }
                            }else{
                                formElementStr = formElementStr + modelAttr.modelAttrName + ": {\n\t\t\tlabel: \"" + fKeyModel.modelName + "\"," + "\n\t\t\t"
                                formElementStr = formElementStr + "holder" + ": " + "\"请选择" + fKeyModel.modelName + "\"," + "\n\t\t\t"
                                if (modelAttr.isRequired == "Y") {
                                    formElementStr = formElementStr + "rules: {" + "\n\t\t\t\t"
                                    formElementStr = formElementStr + "isRequired: " + "\"" + fKeyModel.modelName + "必选\"," + "\n\t\t\t},\n\t\t},\n\t\t"
                                } else {
                                    formElementStr = formElementStr + "rules: {}" + "\n\t\t},"
                                }
                            }
                            
                        }
                    } else {
                        formElementStr = formElementStr + modelAttr.modelAttrName + ": {\n\t\t\tlabel: \"" + modelAttr.attrDesc + "\"," + "\n\t\t\t"
                        formElementStr = formElementStr + "holder" + ": " + "\"请输入" + modelAttr.attrDesc + "\"," + "\n\t\t\t"
                        if (modelAttr.isRequired == "Y") {
                            formElementStr = formElementStr + "rules: {" + "\n\t\t\t\t"
                            formElementStr = formElementStr + "isRequired: " + "\"" + modelAttr.attrDesc + "必填\"," + "\n\t\t\t},\n\t\t},\n\t\t"
                        } else {
                            formElementStr = formElementStr + "rules: {}" + "\n\t\t},"
                        }
                    }
                } else {
                    formElementStr = formElementStr + modelAttr.modelAttrName + ": {\n\t\t\tlabel: \"" + modelAttr.attrDesc + "\"," + "\n\t\t\t";
                    formElementStr = formElementStr + "holder" + ": " + "\"请选择" + modelAttr.attrDesc + "\"," + "\n\t\t\t"

                    if (modelAttr.isRequired == "Y") {
                        formElementStr = formElementStr + "rules: {" + "\n\t\t\t\t"
                        formElementStr = formElementStr + "isRequired: " + "\"" + modelAttr.attrDesc + "必选\"," + "\n\t\t\t}," + "\n\t\t},"
                    } else {
                        formElementStr = formElementStr + "rules: {}" + "\n\t\t},"
                    }
                }

            }
        }

        tableStr = tableStr + "\n\t\t" + modelAttr.modelAttrName + ": " + "\"" + modelAttr.attrDesc + "\","
        if (fKeyStatus == true && fKeyModel) {
            var fKeyTableStr = "";
            var modelAttrListBel = await sysModelAttr.findAll({
                "where": {
                    "modelId": fKeyModel.modelId
                },
                "order": ['sortNo'],
            });
            fKeyTableStr = fKeyTableStr + "\n\t\t" + fKeyBel.refAsName + ": {"
            if(fKeyModel.modelCode=="com.xframe.domain.sys.SysValueDetail"){
                fKeyTableStr= fKeyTableStr+"\n\t\t\t"+'valDetailId: "主键",'+ "\n\t\t\t"+'valCode: "'+modelAttr.attrDesc+'",'+ "\n\t\t\t"+'val: "'+modelAttr.attrDesc+'",'+ "\n\t\t\t"+'text: "'+modelAttr.attrDesc+'"'
                
            }else{
                for (var z = 0; z < modelAttrListBel.length; z++) {
                    if (modelTableRefDis.indexOf(modelAttrListBel[z].modelAttrName) == -1) {
                        fKeyTableStr = fKeyTableStr + "\n\t\t\t" + modelAttrListBel[z].modelAttrName + ": \"" + modelAttrListBel[z].attrDesc + "\","
                    }
                }
            }
            
            fKeyTableStr = fKeyTableStr + "\n\t\t},"

            tableStr = tableStr + fKeyTableStr
        }
    }
    var modelPath = mkdirByPath()
    console.log(model)
    var modelFileName = model.modelAlName.charAt(0).toLowerCase() + model.modelAlName.slice(1);
    var jsStr = "export default {\n\t"+"modelCode:\""+model.modelCode+"\",\n\t" +"title:\""+model.modelName+"详情\",\n\t"+ "query: {" + queryStr + "\n\t},\n\tform: {\n\t\ttabs: {},\n\t\tgroups: {},\n\t\ttitle: \"" + model.modelName + "管理\",\n" + formElementStr + "\n\t},\n\ttable: {" + tableStr + "\n\t}\n}"
    writeJsFile(modelPath + "/" + modelFileName + ".js", jsStr)
    console.log(modelFileName + ".js" + "已生成")
}

module.exports = getModelAttrs;